/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.form;

import dao.ChiTietHoaDonDAO;
import dao.CuaHangDAO;
import dao.HoaDonBanHangDAO;
import dao.KhachHangDAO;
import dao.MatHangDAO;
import dao.NhanVienDAO;
import dao.QuanLySuThayDoiDAO;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;
import pojo.Cuahang;
import pojo.Khachhang;
import pojo.Mathang;
import pojo.Nhavien;

/**
 *
 * @author FT
 */
public class HoaDon extends javax.swing.JFrame {

    /**
     * Creates new form HoaDon
     */
    private DefaultComboBoxModel cbModel2;
    private DefaultComboBoxModel cbModelCuaHang;
    private DefaultComboBoxModel cbKHModel;
    private DefaultComboBoxModel cbModelMatHang;
    private DefaultTableModel tableModel;
    List<Nhavien> listnv = new ArrayList<Nhavien>();
    List<Cuahang> listch = new ArrayList<Cuahang>();
    List<Khachhang> listkh = new ArrayList<Khachhang>();
    List<Mathang> listmh = new ArrayList<Mathang>();
    int stt;
    int thtien = 0;
    int tongtienhd =0;
    int thue = 0;
    public HoaDon() {
        initComponents();
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date date = new Date();
        jtfNgayLap.setText(dateFormat.format(date));
        
        listnv = NhanVienDAO.layDanhSachNhanVien();
        listch = CuaHangDAO.layDanhSachCuaHang();
        listkh = KhachHangDAO.layDanhSachKhachhang();
        listmh = MatHangDAO.layDanhSachMatHang();
        
        cbModel2 = new DefaultComboBoxModel();
        cbModelCuaHang = new DefaultComboBoxModel();
        cbKHModel = new DefaultComboBoxModel();
        cbModelMatHang = new DefaultComboBoxModel();
        tableModel = new DefaultTableModel();
        
        jComboBox1.setModel(cbModel2);
        jcbCH.setModel(cbModelCuaHang);
        jcbKH.setModel(cbKHModel);
        jcbMatHang.setModel(cbModelMatHang);
        
        for (int i=0;i<listnv.size();i++){
            cbModel2.addElement(listnv.get(i).getIdnhavien());
        }
        
        for (int i=0;i<listch.size();i++){
            cbModelCuaHang.addElement(listch.get(i).getIdcuahang());
        }
        
        for (int i=0;i<listkh.size();i++){
            cbKHModel.addElement(listkh.get(i).getIdkhachhang());
        }
        
        for (int i=0;i<listmh.size();i++){
            cbModelMatHang.addElement(listmh.get(i).getIdmathang());
        }
        jLabel16.setText(thtien+"");//Thanh tien
        jLabel18.setText(tongtienhd+"");//Tong tien
        stt=1;
        
        
        tableModel.addColumn("STT");
        tableModel.addColumn("Ma mat hang");
        tableModel.addColumn("Tên mat hang");
        tableModel.addColumn("So luong");
        tableModel.addColumn("Khuyen mai");
        tableModel.addColumn("Don gia");
        tableModel.addColumn("Thanh Tien");
        tableModel.addColumn("Tong Tien");
        jTable1.setModel(tableModel);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtfNgayLap = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jlbName = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jcbCH = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jcbKH = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jcbMatHang = new javax.swing.JComboBox();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jtfSoLuong = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel22 = new javax.swing.JLabel();
        jtfThue = new javax.swing.JTextField();
        jLabel23 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("HÓA ĐƠN BÁN HÀNG");

        jLabel3.setText("Ngày bán: ");

        jtfNgayLap.setText("               ");
        jtfNgayLap.setFocusable(false);

        jLabel4.setText("Nhân viên bán:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel5.setText("Họ và Tên:");

        jlbName.setText("jLabel6");

        jLabel7.setText("Mã cửa hàng: ");

        jcbCH.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcbCH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbCHActionPerformed(evt);
            }
        });

        jLabel8.setText("Cửa hàng:");

        jLabel9.setText("jLabel9");

        jLabel10.setText("Khách hàng:");

        jcbKH.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcbKH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbKHActionPerformed(evt);
            }
        });

        jLabel11.setText("Khách hàng:");

        jLabel12.setText("jLabel12");

        jLabel13.setText("Danh sách các mặt hàng: ");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel14.setText("Thành tiền:");

        jLabel15.setText("Thuế:");

        jLabel17.setText("Tổng tiền:");

        jLabel18.setText("jLabel18");

        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("OK");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel6.setText("Mã mặt hàng:");

        jcbMatHang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcbMatHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbMatHangActionPerformed(evt);
            }
        });

        jLabel19.setText("Tên mặt hàng:");

        jLabel20.setText("jLabel20");

        jLabel21.setText("Số lượng:");

        jtfSoLuong.setText("1");

        jButton3.setText("Thêm");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Xóa");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel22.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel22.setText("Thêm mặt hàng:");

        jtfThue.setText("5");

        jLabel23.setText("%");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(128, 128, 128)
                        .addComponent(jLabel2)
                        .addGap(54, 54, 54)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel7)
                            .addComponent(jLabel10))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtfNgayLap)
                            .addComponent(jComboBox1, 0, 78, Short.MAX_VALUE)
                            .addComponent(jcbCH, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jcbKH, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(18, 18, 18)
                                .addComponent(jlbName))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel9))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel12))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel13))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel15)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfThue, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel23))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton3)
                            .addComponent(jLabel14))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel16)))
                .addContainerGap(145, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel17)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel18))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(189, 189, 189)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton1)))
                        .addGap(92, 130, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel21))
                                .addGap(29, 29, 29)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jcbMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel19)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel20))
                                    .addComponent(jtfSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jButton4)
                            .addComponent(jLabel22)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfNgayLap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jlbName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jcbCH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jcbKH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12))
                .addGap(18, 18, 18)
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel22)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jcbMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel19)
                    .addComponent(jLabel20))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel21)
                    .addComponent(jtfSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel14)
                            .addComponent(jLabel16))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel15)
                            .addComponent(jtfThue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel23))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel17)
                            .addComponent(jLabel18))
                        .addGap(32, 32, 32))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton2)
                            .addComponent(jButton1))
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // TODO add your handling code here:
        
    }//GEN-LAST:event_formWindowActivated

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
         String manv = (String)jComboBox1.getSelectedItem();
         for (int i=0;i<listnv.size();i++){
            if (manv.equals(listnv.get(i).getIdnhavien())){
                jlbName.setText(listnv.get(i).getTennhanvien());
            }
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jcbCHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbCHActionPerformed
        // TODO add your handling code here:
        String mach = (String)jcbCH.getSelectedItem();
         for (int i=0;i<listch.size();i++){
            if (mach.equals(listch.get(i).getIdcuahang())){
                jLabel9.setText(listch.get(i).getTencuahang());
            }
        }
    }//GEN-LAST:event_jcbCHActionPerformed

    private void jcbKHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbKHActionPerformed
        // TODO add your handling code here:
        String makh = (String)jcbKH.getSelectedItem();
         for (int i=0;i<listkh.size();i++){
            if (makh.equals(listkh.get(i).getIdkhachhang())){
                jLabel12.setText(listkh.get(i).getTenkhachhang());
            }
        }
    }//GEN-LAST:event_jcbKHActionPerformed

    private void jcbMatHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbMatHangActionPerformed
        // TODO add your handling code here:
         String mamh = (String)jcbMatHang.getSelectedItem();
         for (int i=0;i<listmh.size();i++){
            if (mamh.equals(listmh.get(i).getIdmathang())){
                jLabel20.setText(listmh.get(i).getTenmathang());
            }
        }
    }//GEN-LAST:event_jcbMatHangActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        Vector vt = new Vector();
        String nsx="";
        String mamh="";
        Date date = new Date();
        
        mamh = (String)jcbMatHang.getSelectedItem();
        vt.add(stt);
        vt.add(jcbMatHang.getSelectedItem());
        vt.add(jLabel20.getText());
        vt.add(jtfSoLuong.getText());
        int soluong = Integer.valueOf(jtfSoLuong.getText());
        String chuoingay = jtfNgayLap.getText();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        try {
            date = sdf.parse(chuoingay);
        } catch (ParseException ex) {
            Logger.getLogger(HoaDon.class.getName()).log(Level.SEVERE, null, ex);
        }
        int a = MatHangDAO.CheckHinhThucKhuyenMai(mamh, soluong, date);
        int giamgia=0;
        if (a==1){
            giamgia = MatHangDAO.GetPhanTramKhuyenMai1(mamh);
            vt.add(giamgia+"%");
        }
        if (a==2)
            vt.add("Tang kem san pham");
        if (a==0){
            vt.add("Khong");
        }
        
        Mathang mh = MatHangDAO.GetMatHang(mamh);
        int gia = mh.getGiaxuat();
        vt.add(gia);
        
        int thanhtien = gia * soluong;
        vt.add(thanhtien+"");
        
        int tongtien = thanhtien - (thanhtien*giamgia)/100;
        vt.add(tongtien+"");
        thtien = thtien + tongtien;
        jLabel16.setText(thtien+"");
        thue = Integer.valueOf(jtfThue.getText());
        tongtienhd = thtien + (thtien * thue)/100;
        jLabel18.setText(tongtienhd+"");
        tableModel.addRow(vt);
        stt++;
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
         int a=jTable1.getSelectedRow();
    
        if (a== -1)
            return;
        tableModel.removeRow(a);
        int index = 1;
        for (int i=0;i<tableModel.getRowCount();i++){
            tableModel.setValueAt(index, i, 0);
            index++;
        }
        stt = index;
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        String mahoadon = HoaDonBanHangDAO.MaTangTuDong();
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        String ngay = jtfNgayLap.getText();
        Date ngayban = new Date();
        try {
            ngayban = dateFormat.parse(ngay);
        } catch (ParseException ex) {
            Logger.getLogger(HoaDon.class.getName()).log(Level.SEVERE, null, ex);
        }
        String nhanvien = (String)jComboBox1.getSelectedItem();
        String cuahang = (String)jcbCH.getSelectedItem();
        String khachahng = (String)jcbKH.getSelectedItem();
        int thue = Integer.valueOf(jtfThue.getText());
        HoaDonBanHangDAO.Insert(mahoadon, ngayban, nhanvien, cuahang, khachahng, thue, thtien);
        String mathaydoi = QuanLySuThayDoiDAO.MaTangTuDong();
        QuanLySuThayDoiDAO.Insert(mathaydoi, ngayban, mahoadon, 1, Login.manhanvien, "hoa don");
        int sodong = tableModel.getRowCount();
        for (int i=0;i<sodong;i++){
            String machitiethd = ChiTietHoaDonDAO.MaTangTuDong();
            String mathang = (String)tableModel.getValueAt(i, 1);
            int sl=0;
            sl = Integer.valueOf((String)tableModel.getValueAt(i, 3));
            System.out.println("Tien: "+tableModel.getValueAt(i, 7)+"a");
            int tien =0; 
            String tt = (String)tableModel.getValueAt(i, 7);
            tien = Integer.valueOf(tt);
            ChiTietHoaDonDAO.Insert(machitiethd, mahoadon, mathang, sl, tien);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(HoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(HoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(HoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(HoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HoaDon().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JComboBox jcbCH;
    private javax.swing.JComboBox jcbKH;
    private javax.swing.JComboBox jcbMatHang;
    private javax.swing.JLabel jlbName;
    private javax.swing.JTextField jtfNgayLap;
    private javax.swing.JTextField jtfSoLuong;
    private javax.swing.JTextField jtfThue;
    // End of variables declaration//GEN-END:variables
}
